#include "layerconfigdialog.h"
#include "ui_layerconfigdialog.h"
#include <QFileDialog>

LayerConfigDialog::LayerConfigDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LayerConfigDialog),
    m_layer()
{
    ui->setupUi(this);
    setupUI();
    
    // 设置默认值
    ui->nameLineEdit->setText(tr("新图层"));
    ui->layerTypeComboBox->setCurrentIndex(0);
    ui->sourceLineEdit->setText("");
    ui->visibleCheckBox->setChecked(true);
}

LayerConfigDialog::LayerConfigDialog(const MapLayer &layer, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LayerConfigDialog),
    m_layer(layer)
{
    ui->setupUi(this);
    setupUI();
    
    // 填充现有图层信息
    ui->nameLineEdit->setText(layer.name());
    ui->layerTypeComboBox->setCurrentIndex(layer.type());
    ui->sourceLineEdit->setText(layer.source());
    ui->visibleCheckBox->setChecked(layer.isVisible());
    
    // 根据图层类型更新界面
    on_layerTypeComboBox_currentIndexChanged(layer.type());
}

LayerConfigDialog::~LayerConfigDialog()
{
    delete ui;
}

MapLayer LayerConfigDialog::layer() const
{
    return m_layer;
}

void LayerConfigDialog::setupUI()
{
    // 设置图层类型下拉框
    ui->layerTypeComboBox->addItem(tr("网格图层"), MapLayer::GridLayer);
    ui->layerTypeComboBox->addItem(tr("MBTiles图层"), MapLayer::MBTilesLayer);
    ui->layerTypeComboBox->addItem(tr("高德地图"), MapLayer::AmapLayer);
    ui->layerTypeComboBox->addItem(tr("天地图"), MapLayer::TianDiTuLayer);
    
    // 连接信号槽
    connect(ui->layerTypeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &LayerConfigDialog::on_layerTypeComboBox_currentIndexChanged);
}

void LayerConfigDialog::on_layerTypeComboBox_currentIndexChanged(int index)
{
    // 根据图层类型显示或隐藏浏览按钮
    MapLayer::LayerType type = static_cast<MapLayer::LayerType>(index);
    bool showBrowseButton = (type == MapLayer::MBTilesLayer || type == MapLayer::GridLayer);
    ui->browseButton->setVisible(showBrowseButton);
    
    // 为在线地图设置默认URL
    if (type == MapLayer::AmapLayer) {
        ui->sourceLineEdit->setText("https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}");
    } else if (type == MapLayer::TianDiTuLayer) {
        ui->sourceLineEdit->setText("http://t{s}.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}");
    }
}

void LayerConfigDialog::on_browseButton_clicked()
{
    QString filter;
    if (ui->layerTypeComboBox->currentIndex() == MapLayer::MBTilesLayer) {
        filter = tr("MBTiles文件 (*.mbtiles);;所有文件 (*.*)");
    } else {
        filter = tr("所有文件 (*.*)");
    }
    
    QString filePath = QFileDialog::getOpenFileName(this, tr("选择文件"), "", filter);
    if (!filePath.isEmpty()) {
        ui->sourceLineEdit->setText(filePath);
    }
}

void LayerConfigDialog::on_buttonBox_accepted()
{
    // 更新图层信息
    m_layer.setName(ui->nameLineEdit->text());
    m_layer.setType(static_cast<MapLayer::LayerType>(ui->layerTypeComboBox->currentIndex()));
    m_layer.setSource(ui->sourceLineEdit->text());
    m_layer.setVisible(ui->visibleCheckBox->isChecked());
}